#include<iostream>
using namespace std;
#include<vector>
#include<unordered_map>
class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        unordered_map<int, int> find;
        int n = arr.size(), ans = 0;
        vector<vector<int>> dp(n, vector<int>(n));
        find[arr[0]] = 0;
        find[arr[1]] = 1;
        for (int i = 2; i < n; ++i) {
            for (int j = i - 1; j >= 1; --j) {
                if (arr[i] < 2 * arr[j] && find.count(arr[i] - arr[j]))
                    dp[j][i] = max(dp[j][i],dp[find[arr[i] - arr[j]]][j] + 1);
                ans = max(ans, dp[j][i]);
            }
            find[arr[i]] = i;
        }
        return ans==0?0:ans+2;
    }
};